package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.LpsOrderPjService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.LpsOrderPjForm;
import com.youlai.boot.system.model.query.LpsOrderPjQuery;
import com.youlai.boot.system.model.vo.LpsOrderPjVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 配件前端控制层
 *
 * @author lwb
 * @since 2025-07-14 16:22
 */
@Tag(name = "配件接口")
@RestController
@RequestMapping("/api/v1/lpsOrderPjs")
@RequiredArgsConstructor
public class LpsOrderPjController  {

    private final LpsOrderPjService lpsOrderPjService;

    @Operation(summary = "配件分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:lpsOrderPj:query')")
    public PageResult<LpsOrderPjVO> getLpsOrderPjPage(LpsOrderPjQuery queryParams ) {
        IPage<LpsOrderPjVO> result = lpsOrderPjService.getLpsOrderPjPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增配件")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:lpsOrderPj:add')")
    public Result<Void> saveLpsOrderPj(@RequestBody @Valid LpsOrderPjForm formData ) {
        boolean result = lpsOrderPjService.saveLpsOrderPj(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取配件表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:lpsOrderPj:edit')")
    public Result<LpsOrderPjForm> getLpsOrderPjForm(
        @Parameter(description = "配件ID") @PathVariable Long id
    ) {
        LpsOrderPjForm formData = lpsOrderPjService.getLpsOrderPjFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改配件")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:lpsOrderPj:edit')")
    public Result<Void> updateLpsOrderPj(
            @Parameter(description = "配件ID") @PathVariable Long id,
            @RequestBody @Validated LpsOrderPjForm formData
    ) {
        boolean result = lpsOrderPjService.updateLpsOrderPj(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除配件")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:lpsOrderPj:delete')")
    public Result<Void> deleteLpsOrderPjs(
        @Parameter(description = "配件ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = lpsOrderPjService.deleteLpsOrderPjs(ids);
        return Result.judge(result);
    }
}
